Dynamic Dropdown Menu

By da^hype on Oct 18, 2006

A function to build a basic dynamic dropdown menu.

<?php

/*
 * Dynamic Dropdown Menu
 * by da^hype (shahir reza ali)
 * www.codedb.org
 * 12/10/2006
 *
 * $fieldname = Name of the field
 * $array =  An associative array.
 * $firstentry = First option in dropdown
 * $lastentry = Last option in dropdown
 * $onchange = $onchange designates a JavaScript to run when the user chooses one of the options.
 * $selectedkey = Default selected item
 *
 * Example:
 * $_array = array('1'=> 'one', '2'=> 'two', '3'=> 'three');
 *  echo create_dropdown("numbers", $_array, "Select One", "Dont Know..", FALSE, 2);
 *
 */
function create_dropdown($fieldname, $array, $firstentry = FALSE, $lastentry = FALSE, $onchange = FALSE, $selectedkey = " ")
 {
   $dropdown = ($onchange != FALSE) ? '<select size=1 name="'.$fieldname.'" onchange="'.$onchange.'">' :
                '<select size=1 name="'.$fieldname.'">';

   if ($firstentry != FALSE) //if $firstentry is not FALSE, set one!!!
       $dropdown .= '<option value="0">'.$firstentry.'</option>';

   foreach($array AS $value => $name) //loop threw array to create the menu
   {
        $dropdown .= ($value == $selectedkey) ?
                      '<option value="'.$value.'" selected="'.$selectedkey.'">'.$name.'</option>' :
                      '<option value="'.$value.'">'.$name.'</option>';
   }

   if ($lastentry != FALSE) //if $lastentry is not false, set one!!!
       $dropdown .= '<option value="-1">'.$lastentry.'</option>';

   $dropdown .= '</select>';

  return $dropdown;  //return the dropdown menu
}

?>

Comments

Sign in to comment.
qakbar   -  Jun 21, 2010

Hi,

Is their an example with css on how this works??? I want ot create a dropdown css based menu for a learning purposes, the tutorials I have are using table structure and basic vertical menu system that is crap. I want ot learn how to design horizontal dropdown menu. I can describe what i mean.

Subjects: (i.e. Home, About us, Contact us)
id (Int 11 - Primary, Auto)
menu_name (varchar - 30)
Content - (Text)

Pages: (i.e. Related to Subjects not all subjects to have pages with them)
id (Int 11 - Primary Auto)
Subject_id (Int 11 - [related to subjects])
memu_name (Varchar 30)
Content (Text)

Any help provided is much appreciated, my email is qakbar@hotmail.co.uk

Thank you Qaysar

 Respond  
da^hype   -  Oct 18, 2006

You dont have escape quotes to display $variables in strings \"hello $nickname\" works fine. Only when your referencing an array do you need to \'..\'

  • i know, it\'s just a habit i have. :)
 Respond  
Mpdreamz   -  Oct 18, 2006

I hate standard dropdowns CSS has hardly any grasp on em and they show trough DIV\'s positioned over them in IE. (Thats why in my projects i use my own custum built one).

You dont have escape quotes to display $variables in strings \"hello $nickname\" works fine. Only when your referencing an array do you need to \'..\'

 Respond  
Are you sure you want to unfollow this person?
Are you sure you want to delete this?
Click "Unsubscribe" to stop receiving notices pertaining to this post.
Click "Subscribe" to resume notices pertaining to this post.